//
//  RVHomeViewController.m
//  RememberVocabularies
//
//  Created by 伍银荣 on 2017/10/14.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "RVHomeViewController.h"
#import "RVSearchViewController.h"
#import "RVChangeplanViewController.h"
#import "RVReciteWordsViewController.h"
#import "wordsModel.h"

@interface RVHomeViewController ()<RVChangeplanViewControllerDelegate>

@property (strong, nonatomic) IBOutlet UIView *homeView;

//剩余天数
@property (weak, nonatomic) IBOutlet UILabel *remainDaysLabel;
//今日背单词个数
@property (weak, nonatomic) IBOutlet UILabel *todaywordsLabel;
//书籍类型
@property (weak, nonatomic) IBOutlet UILabel *booktypeLabel;
//改计划
@property (weak, nonatomic) IBOutlet UIButton *changeplanBtn;
//已完成单词个数
@property (weak, nonatomic) IBOutlet UILabel *finishedwordsLabel;
//单词总个数
@property (weak, nonatomic) IBOutlet UILabel *wordsNumLabel;
//进度条
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
//开始背单词按钮
@property (weak, nonatomic) IBOutlet UIButton *writtingBeginBtn;


@end

@implementation RVHomeViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //取值
    NSDictionary *value = [userDefaults valueForKey:@"nowPlan"];
    self.remainDaysLabel.text = value[@"day"];
    self.todaywordsLabel.text = value[@"wordNum"];
    self.booktypeLabel.text = value[@"bookName"];
    NSString *key = value[@"bookPath"];
    NSArray *arr = [[NSDictionary dictionaryWithContentsOfFile:finishPlist] valueForKey:key];
    self.finishedwordsLabel.text = [NSString stringWithFormat:@"%ld",arr.count];
    self.wordsNumLabel.text = [NSString stringWithFormat:@"/%@",value[@"bookWordNum"]];
    
    if (self.booktypeLabel.text.length == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"欢迎使用,请选择您的计划." preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"进入" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self changeplanBtnClick];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    }
    float progress = [self.finishedwordsLabel.text floatValue] / [value[@"bookWordNum"] floatValue];
    NSLog(@"%f",progress);
   self.progressView.progress = progress;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
}



-(void)setupUI
{
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 30)];
    [rightButton setImage:[UIImage imageNamed:@"Feed_SearchBtn_18x18_"] forState:UIControlStateNormal];
    [rightButton setTitle:@"词典" forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    
    rightButton.layer.borderWidth = 0.5;
    rightButton.layer.borderColor = [UIColor whiteColor].CGColor;
    rightButton.layer.cornerRadius = 15;
    rightButton.layer.masksToBounds = YES;
    
    [rightButton addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _changeplanBtn.layer.borderWidth = 0.5;
    _changeplanBtn.layer.borderColor = MRGlobalBg.CGColor;
    _changeplanBtn.layer.cornerRadius = 15;
    _changeplanBtn.layer.masksToBounds = YES;
    [_changeplanBtn addTarget:self action:@selector(changeplanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    _writtingBeginBtn.layer.cornerRadius = 15;
    _writtingBeginBtn.layer.masksToBounds = YES;

    _homeView.layer.cornerRadius = 15;
    _homeView.layer.masksToBounds = YES;

}

-(void)changeplanBtnClick
{
    RVChangeplanViewController *changeplanVC = [[RVChangeplanViewController alloc]init];
    changeplanVC.delegate = self;
    [self.navigationController pushViewController:changeplanVC animated:YES];
}

-(void)backValue:(NSDictionary *)value
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //保存内容
    [userDefaults setObject:value forKey:@"nowPlan"];
    
    NSLog(@"--------%@", [userDefaults valueForKey:@"aaa"]);
    NSLog(@"--------%@", [userDefaults valueForKey:@"nowPlan"]);
    NSLog(@"path------->%@",value[@"bookPath"]);
    
    self.remainDaysLabel.text = value[@"day"];
    self.todaywordsLabel.text = value[@"wordNum"];
    self.booktypeLabel.text = value[@"bookName"];
    self.finishedwordsLabel.text = @"100";
    self.wordsNumLabel.text = [NSString stringWithFormat:@"/%@",value[@"bookWordNum"]];
    self.progressView.progress = [self.finishedwordsLabel.text integerValue] / [value[@"bookWordNum"] integerValue];
}


-(void)rightBtnClick
{
    RVSearchViewController *searchVC = [[RVSearchViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (IBAction)reciteWordsBtn:(id)sender {
    
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] valueForKey:@"nowPlan"];
    NSString *bookPath = [dict valueForKey:@"bookPath"];
    //获取书籍文件路径
    NSString *path = [[NSBundle mainBundle]pathForResource:bookPath ofType:@"json"];
    //读取路径文件
    NSData *bookData = [NSData dataWithContentsOfFile:path];
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:bookData options:NSJSONReadingAllowFragments error:nil];
    
    NSMutableArray *bookMarr = [NSMutableArray array];
    for (NSDictionary *dict in arr) {
        wordsModel *wordModel = [wordsModel wordItemWithDict:dict];
        [bookMarr addObject:wordModel];
    }
    NSMutableArray *marr = [NSMutableArray array];
    NSInteger sum = [[NSUserDefaults standardUserDefaults] integerForKey:@"sum"];
    for (NSInteger i = sum; i < [dict[@"wordNum"] integerValue] + sum; i++) {
        [marr addObject:bookMarr[i]];
    }
    NSLog(@"sum-------》%ld",sum);
    RVReciteWordsViewController *reciteWordsVC =[RVReciteWordsViewController new];
    //传值1
    reciteWordsVC.wordsArr = marr;
    [self.navigationController pushViewController:reciteWordsVC animated:YES];
}

@end
